<?php
defined('IN_IXDCW') or exit('Access Denied');
$menus = array (
    array('商户列表', '?moduleid='.$moduleid.'&file='.$file),
);
$do = new Business();
$businessAuthDb = Db('businessAuth');
$this_forward = '?moduleid='.$moduleid.'&file='.$file;

if(in_array($action, array(''))) {
	$sfields = array('会员名');
	$dfields = array('m.username');

	isset($fields) && isset($dfields[$fields]) or $fields = 0;

	$fields_select = dselect($sfields, 'fields', '', $fields);

	$condition = '';
	if($kw) $condition .= " AND $dfields[$fields] LIKE '%$kw%'";
	$pagesize = !empty($psize) ? intval($psize) : 20;
	if (!isset($verify)) {
		$verify = 1;
	}
	if ($verify != 2) {
		$condition .=' and a.verify='.$verify;
	}
}
switch($action) {
	case 'verify':
		if ($post['username']) {
			$username = trim($post['username']);
			$username or msg('请输入需要加入权限的用户名!');
			$memberDb = Db('member');
			$user = $memberDb->getByUsername($username);
			if (!$user) {
				msg('不存在的用户'.$username.'!');
			}
			$businessAuthDb->verify($user['userid']) or msg('添加商户权限失败, 请联系管理员', $this_forward);
			msg('添加商户成功', $this_forward);
		} else {
			msg('请输入正确的用户名!');
		}
		break;
	case 'unverify':
		if ($post['username']) {
			$username = trim($post['username']);
			$username or msg('请输入需要取消权限的用户名!');
			$memberDb = Db('member');
			$user = $memberDb->getByUsername($username);
			if (!$user) {
				msg('不存在的用户'.$username.'!');
			}
			$businessAuthDb->unVerify($user['userid']) or msg('取消商户权限失败, 请联系管理员', $this_forward);
			msg('取消商户权限成功!', $this_forward);
		} else {
			msg('请输入正确的用户名!');
		}
		break;
	case 'store':
		$sfields = array('会员名', '商户名称');
		$dfields = array('username', 'shopname');
		isset($fields) && isset($dfields[$fields]) or $fields = 0;
		$fields_select = dselect($sfields, 'fields', '', $fields);
		$isMainStore = empty($mainStore) ? false :true;

		$condition = ' 1=1 ';
		$condition .= $isMainStore ? ' and mainstore = 1' : '';
		$companys = $do->storeList($condition);
		include tpl('business_store', $module);
		break;
	case 'delete':
		$itemid or msg(errmsg);
		$do->delete($itemid);
		dheader('?moduleid=2&file=business&action=store');
		break;
	default:
		$num = $businessAuthDb->getCount($condition);

		$pn = new Pagination();
		$pn->recordsPerPage($pagesize);
		$pn->records($num);
		$condition .= " LIMIT " . ($pn->getPage() - 1) * $pagesize . ',' . $pagesize;
		$lists = $businessAuthDb->getList($condition);
		include tpl('business_auth', $module);
	break;
}